package com.baosight.sczx.zm.cb.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCSNumber;
import com.baosight.sczx.common.zm.cInterface.cb.Coalseam;
import com.baosight.sczx.zm.cb.domain.ZMCBWorkingface;
import com.baosight.sczx.zm.cb.domain.ZMCBCoalseam;

/**
 * <p>Title: ServiceZMCB10.java</p>
 * <p>Description: 煤层信息管理-煤层信息类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 王开兵
 * @version 1.0
 * <pre>Histroy:
 *          2013-6-25 下午14:20:41  王开兵  Create
 *</pre>
 */
public class ServiceZMCB10 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{

//		String fdEngineeringDesignId = inInfo.getString("fdParentId");//模块ID
//		StaticTools.setQueryParameters(inInfo, "fdEngineeringDesignId", fdEngineeringDesignId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCBCoalseam.query", new ZMCBCoalseam());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineStabilty", true));//稳定性
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineType", true));//煤种
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.dipType",true));//倾斜类型
//		outInfo.set("numberType", SystemSettingCS.getInstance().getRoadwayNo());//巷道编号形式 0自动 1手动 2生成
		
		
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
//	 	EiInfo out=new EiInfo(); 
//		List list = inInfo.getBlock("result").getRows();
//		Iterator it = list.iterator();
//		
//		while (it.hasNext()) {
//			Map tagMap = (Map) it.next();
//			boolean validate = validate(tagMap, "ZMCBCoalseam.validate", new HashMap<String, String>());
//			
//			if(validate) 
//			{
//				out = this.query(inInfo);
//				out.setStatus(-1);
//				out.setMsg("重复"); 
//				return inInfo;
//			}
//			else 
//			{
//				String keyId = TablePk.getInstance().getNextValue("T_CI_COALSEAM");
//				tagMap.put("fdKeyId", keyId);//主键
//				dao.insert("ZMCBCoalseam.insert", tagMap);
//			}
//		}
//		out = this.query(inInfo);
//		out.setStatus(1);
//		out.setMsg("操作" + list.size() + "记录成功");
//		return out; 
		

		EiInfo out=new EiInfo(); 
		ZMCBCoalseam _dom = new ZMCBCoalseam();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate) {
				return inInfo;
			} 
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_COALSEAM"));
		    this.dao.insert("ZMCBCoalseam.insert", _dom);
			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
//		EiInfo out=new EiInfo(); 
//		List list = inInfo.getBlock("result").getRows();
//		Iterator it = list.iterator();
//		
//		while (it.hasNext()) {
//			Map tagMap = (Map) it.next();
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("fdKeyId", tagMap.get("fdKeyId").toString());
//		//校验名称、简称是否重复
////		Map<String, String> params = new HashMap<String, String>();
////		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
////		params.put("fdKeyId",tagMap.get("fdKeyId").toString());
//		
//		boolean validate = validate(tagMap, "ZMCBCoalseam.validate", params);
//		if(validate)
//		{
//			return inInfo;
//		}
//		else 
//		{
//			dao.update("ZMCBCoalseam.update", tagMap);
////			EiInfo outInfo = super.update(inInfo, "ZMCBCoalseam.update");
////			return outInfo;
//		}
//		
//	}
//		out = this.query(inInfo);
//		out.setStatus(1);
//		out.setMsg("操作" + list.size() + "条记录成功");
//		return out;
		EiInfo out=new EiInfo();
		ZMCBCoalseam _dom = new ZMCBCoalseam();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(inInfo, _dom, params);
			if(validate) {
				out = this.query(inInfo);
				return out;
			} 
		    this.dao.update("ZMCBCoalseam.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
		
		
		
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
//		EiInfo out=new EiInfo();
//		List list = inInfo.getBlock("result").getRows();
//		Iterator it = list.iterator();
//		while (it.hasNext()) {
//			Map tagMap = (Map) it.next();
//			dao.delete("ZMCBCoalseam.delete", tagMap);	
//					
//		}
//		out = this.query(inInfo);
//		out.setStatus(1);
//		out.setMsg("操作" + inInfo.getBlock("result").getRowCount() + "条记录成功");
//		return out; 
		
		
		
		
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			_list.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		params.put("fdKeyIds", _list);
		
		//该煤层是否已被使用
		boolean validate = Coalseam.validateUsed(params, inInfo);
		if(validate)
		{
			return inInfo;
		}
		else
		{
			EiInfo _temp = super.delete(inInfo, "ZMCBCoalseam.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 校验名称编号不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午14:34:17 王开兵
	 * </p>
	 *
	 */

	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCBCoalseam _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdNo = _dom.getFdNo();
		String fdName = _dom.getFdName();
		//String fdAbbreviation = inInfo.getBlock("result").getCellStr(0, "fdAbbreviation");
		
		params.put("fdNo", fdNo);
		params.put("fdName", fdName);
		//params.put("fdAbbreviation", fdAbbreviation);
		
		List result = dao.query("ZMCBCoalseam.validate", params);
		EiInfo out=new EiInfo();
		if(result != null && result.size() > 0) {
			out.setStatus(-1);
			ZMCBCoalseam _temp = (ZMCBCoalseam)result.get(0);
		
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)){
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}

		msg.append("重复！");
		
		out.setMsg(msg.toString());
			return true;
		}
		return false;
	}
}
